#include "querypage.h"
#include "ui_querypage.h"
#include <QMessageBox>
#include <QInputDialog>

QueryPage::QueryPage(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::QueryPage)
{
    ui->setupUi(this);
}

QueryPage::~QueryPage()
{
    delete ui;
}

QStringList QueryPage::ActivityTypeNames(ActivityType type) {
    QStringList typeNames;
    if(type==noneType) typeNames.append(tr("None"));
    if(type&studentWork) typeNames.append(tr("Student Work"));
    if(type&personalDevelopment) typeNames.append(tr("Personal Development"));
    if(type&practiceVisit) typeNames.append(tr("Practice and Visit"));
    if(type&voluntaryActivity) typeNames.append(tr("Voluntary Activity"));
    if(type&communityLife) typeNames.append(tr("Community Life"));
    return typeNames;
}

void QueryPage::initialization(QString ID) {
    Student student = students[ID];
    QList<Activity> actList = activities.values();

    ui->messageEdit->setPlainText(tr("Student ") + student.name + tr("(ID:")+ID+tr("), your Preliminary School of Communist Youth League credit informations are as follows:"));

    ui->stuWorkWid->clear();
    ui->perDevWid->clear();
    ui->volActWid->clear();
    ui->pracVisWid->clear();
    ui->comLifeWid->clear();
    ui->noneWid->clear();

    ui->creditTreeWid->clear();
    foreach(Activity activity,actList) {
        if(activity.type==noneType) ui->noneWid->addItem(activity.name);
        if(activity.type&studentWork) ui->stuWorkWid->addItem(activity.name);
        if(activity.type&personalDevelopment) ui->perDevWid->addItem(activity.name);
        if(activity.type&voluntaryActivity) ui->volActWid->addItem(activity.name);
        if(activity.type&practiceVisit) ui->pracVisWid->addItem(activity.name);
        if(activity.type&communityLife) ui->comLifeWid->addItem(activity.name);

        QTreeWidgetItem* activityItem = new QTreeWidgetItem({activity.name,ActivityTypeNames(activity.type).join('|')});
        bool hasRecord=false;
        foreach(QString recName,activity.records.keys()) {
            if(activity.records[recName].contains(ID)) {
                CreditRecord rec = activity.records[recName][ID];
                hasRecord = true;
                QTreeWidgetItem* recordItem = new QTreeWidgetItem({recName,
                                                                   ActivityTypeNames(ActivityType(rec.second&activity.type)).join('|'),
                                                                   QString::number(rec.first)});
                activityItem->addChild(recordItem);
            }
        }
        if(hasRecord)
            ui->creditTreeWid->addTopLevelItem(activityItem);
    }
    ui->creditTreeWid->expandAll();

    unsigned total = getTotalCredits(student, actList);
    categoryCreditStatus status = checkTypeStatus(student, actList);
    bool qualified = (total>=fullCredit && status.qualification()==allType);

    if(qualified) {
        ui->QuaEdit->setTextColor(QColor::fromRgb(0,255,0));
        ui->QuaEdit->setPlainText(tr("qualified"));
        ui->QuaEdit->setAlignment(Qt::AlignCenter);
    } else {
        ui->QuaEdit->setTextColor(QColor::fromRgb(255,0,0));
        ui->QuaEdit->setPlainText(tr("not qualified"));
        ui->QuaEdit->setAlignment(Qt::AlignCenter);
    }
    ui->TotProg->setMinimum(0);
    ui->TotProg->setMaximum(qMax(total,(unsigned) fullCredit));
    ui->TotProg->setValue(total);
    ui->SWProg->setMinimum(0);
    ui->SWProg->setMaximum(qMax(status.stuWork,(unsigned) categoryCredit));
    ui->SWProg->setValue(status.stuWork);
    ui->PDProg->setMinimum(0);
    ui->PDProg->setMaximum(qMax(status.perDev,(unsigned) categoryCredit));
    ui->PDProg->setValue(status.perDev);
    ui->VAProg->setMinimum(0);
    ui->VAProg->setMaximum(qMax(status.volAct,(unsigned) categoryCredit));
    ui->VAProg->setValue(status.volAct);
    ui->PVProg->setMinimum(0);
    ui->PVProg->setMaximum(qMax(status.pracVis,(unsigned) categoryCredit));
    ui->PVProg->setValue(status.pracVis);
    ui->CLProg->setMinimum(0);
    ui->CLProg->setMaximum(qMax(status.comLife,(unsigned) categoryCredit));
    ui->CLProg->setValue(status.comLife);
}

void QueryPage::on_homeBtn_clicked()
{
    if(home && home!=nullptr)
        home->show();
    this->destroy();
}


void QueryPage::on_queryEdit_editingFinished()
{
    QString queryText = ui->queryEdit->text();
    if(queryText.isEmpty()) return;
    if(!students.contains(queryText)) {
        QMessageBox::warning(this,
                             tr("Warning"),
                             tr("Your ID is not in the list, please inform us for details."),
                             QMessageBox::Ok);
        return;
    }
    if(!students[queryText].pwd.isEmpty()) {
        bool ok;
        QString pwd = QInputDialog::getText(this,
                                            tr("Password"),
                                            tr("Input password:"),
                                            QLineEdit::Password,
                                            {},
                                            &ok,
                                            windowFlags()|Qt::FramelessWindowHint
                                            );
        if(!ok) return;
        if(sha256encode(pwd)!=students[queryText].pwd) {
            QMessageBox::warning(this,
                                 tr("Warning"),
                                 tr("Wrong password!"),
                                 QMessageBox::Ok);
            return;
        }
    }
    initialization(queryText);
}

